package com.tth.learn.addproperties.java;

import org.junit.Test;

import java.util.*;

/**
 * 使用Iterator遍历集合
 * @Author tth
 * @Date 2022/12/12 15:22
 */
public class IteratorTest {

    @Test
    public void test() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(new String("hello"));
        coll.add(null);
        coll.add(new Person("Jerry", 18));

        coll.add(new Student("XiaoMing", 10, 80));

        Iterator iterator = coll.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    @Test
    public void test2() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(new String("hello"));
        coll.add(new Person("Jerry", 18));
        coll.add(new Student("XiaoMing", 10, 80));

        Iterator iterator = coll.iterator();
        while (iterator.hasNext()) {
            Object o = iterator.next();
            if ("hello".equals(o)) {
                iterator.remove();
            }
        }

        Iterator iterator1 = coll.iterator();
        while (iterator1.hasNext()) {
            System.out.println(iterator1.next());
        }

    }

    @Test
    public void test3() {
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.remove(2); // 删除的是第二个元素
        System.out.println(list);
        list.remove(new Integer(5)); // 删除的是元素5
        System.out.println(list);
    }
}
